<template>
  <div>
    <headerI title="新增收货地址" />
    <div class="division"></div>
    <div class="message">
      <div class="name">
        <span>收货人</span>
        <input
          type="text"
          name=""
          id=""
          v-model="name"
          placeholder="请输入姓名"
        />
      </div>
      <div class="partition"></div>
      <div class="number">
        <span>手机号码</span>
        <input
          type="text"
          name=""
          id=""
          v-model="number"
          placeholder="请输入手机号"
        />
      </div>
      <div class="partition"></div>
      <div class="region">
        <span class="tit">选择地区</span>
        <div class="region-box">
          <span class="text">{{ this.add }}</span>
          <img
            src="../../assets/img/address-manage/add address_icon_forward@2x.png"
            alt=""
            @click="showPopup"
          />
        </div>
      </div>
      <div class="partition"></div>
      <div class="address">
        <span>详细地址</span>
        <input
          type="text"
          name=""
          id=""
          v-model="address"
          placeholder="如街道、小区、楼栋号、门牌号"
        />
      </div>
    </div>
    <van-popup v-model="show" position="bottom" :style="{ height: '50%' }">
      <van-area
        :area-list="areaList"
        :columns-num="3"
        ref="myArea"
        title="标题"
        @change="onChange"
        @confirm="onConfirm"
        @cancel="onCancel"
      />
    </van-popup>
    <span class="augment" @click="save()">保存并使用</span>
  </div>
</template>

<script>
import headerI from "../../components/indent-header.vue";
import { areaList } from "@vant/area-data";

//cnpm i @vant/area-data
export default {
  name: "newaddress",
  components: {
    headerI,
  },
  data() {
    return {
      areaList,
      show: false,
      carmodel: "",
      add: "省份/城市、区县",
      name: "",
      number: "",
      address: "",
      arr: [],
    };
  },
  methods: {
    // 弹出层显示
    showPopup() {
      this.show = true;
    },
    //value=0改变省，1改变市，2改变区
    onChange(picker) {
      let val = picker.getValues();
      console.log(val); //查看打印
      let areaName = "";
      for (var i = 0; i < val.length; i++) {
        areaName = areaName + (i == 0 ? "" : "/") + val[i].name;
      }
      this.carmodel = areaName;
    },
    //确定选择城市
    onConfirm(val) {
      this.add = val[0].name + "," + val[1].name + "," + val[2].name;
      this.show = false; //关闭弹框
    },
    //取消选中城市
    onCancel() {
      this.show = false;
      this.$refs.myArea.reset(); // 重置城市列表
    },
    save() {
      this.arr.push(this.name);
      this.arr.push(this.number);
      this.arr.push(this.add);
      this.arr.push(this.address);
      if (this.$route.query.id) {
        this.arr.push(this.$route.query.id);
      } else {
        this.arr.push(this.$route.query.arr.length + 1);
      }
      this.$router.push({
        path: `/addressManage`,
        query: {
          arr: this.arr,
          arr1: this.$route.query.arr,
        },
      });
    },
  },
  mounted() {
    console.log(this.$route.query.arr);
  },
};
</script>

<style lang="less" scoped>
.division {
  height: 0.12rem;
  background: #f5f5f5;
}
.message {
  padding-top: 0.12rem;
  margin-left: 0.16rem;
  .partition {
    width: 3.59rem;
    height: 0px;
    border: 0.01rem solid #f5f5f5;
    margin-top: 0.16rem;
    margin-bottom: 0.17rem;
  }
  .name {
    display: flex;
    align-items: center;
    span {
      .span();
    }
    input {
      .input();
      margin-left: 0.46rem;
    }
  }
  .number {
    .name();
    input {
      margin-left: 0.32rem;
    }
  }
  .region {
    display: flex;
    align-items: center;
    .region-box {
      display: flex;
      align-items: center;
      margin-left: 0.32rem;
      justify-content: space-between;
      width: 2.55rem;
      .text {
        display: block;
        font-size: 0.12rem;
        font-family: Source Han Sans CN;
        color: #999999;
      }
      img {
        width: 0.12rem;
        height: 0.12rem;
      }
    }
    .tit {
      .span();
    }
  }
  .address {
    .number();
  }
}
.span {
  display: block;
  font-size: 0.14rem;
  font-family: Source Han Sans CN;
  color: #333333;
}
.input {
  border: none;
  font-size: 0.12rem;
  font-family: Source Han Sans CN;
  color: #999999;
  height: 0.17rem;
}
.augment {
  display: block;
  width: 3.43rem;
  background: #1b7373;
  padding: 0.11rem 0 0.12rem;
  font-size: 0.16rem;
  font-family: PingFang SC;
  font-weight: bold;
  color: #ffffff;
  text-align: center;
  position: fixed;
  bottom: 0.4rem;
  left: 0.16rem;
}
</style>